package com.piik.mall.auth.controller;

import com.piik.common.constant.AuthConstant;
import com.piik.common.dto.MemberDTO;
import com.piik.common.utils.HttpUtils;
import com.piik.common.utils.R;
import com.piik.mall.auth.feign.MemberFeignService;
import com.piik.common.dto.SocialUserDTO;
import org.apache.http.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSON;

import org.apache.http.util.EntityUtils;


import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
public class OAuth2Controller {


    @Autowired
    private MemberFeignService memberFeginService;

    /**
     * 微博账号登录
     * @param code
     * @param session
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("/oauth2/weibo/success")
    public String weiboOAuth(@RequestParam("code") String code
            , HttpSession session, HttpServletResponse response) throws Exception {
        System.out.println("code = " + code);
        Map<String,String> body = new HashMap<>();
        body.put("client_id","3255002568");
        body.put("client_secret","0ed7d488f17a3ac940138f8400b16c34");
        body.put("grant_type","authorization_code");
        body.put("redirect_uri","http://auth.piik.com/oauth2/weibo/success");
        body.put("code",code);
        // 根据Code获取对应的Token信息
        HttpResponse post = HttpUtils.doPost("https://api.weibo.com"
                , "/oauth2/access_token"
                , "post"
                , new HashMap<>()
                , null
                , body
        );
        int statusCode = post.getStatusLine().getStatusCode();
        if(statusCode != 200){
            // 说明获取Token失败,就调回到登录页面
            return "redirect:http://auth.piik.com/login.html";
        }
        // 说明获取Token信息成功
        String json = EntityUtils.toString(post.getEntity());
        SocialUserDTO socialUser = JSON.parseObject(json, SocialUserDTO.class);
        socialUser.setSocialType(1);
        R r = memberFeginService.socialLogin(socialUser);
        if(r.getCode() != 0){
            // 登录错误
            return "redirect:http://auth.piik.com/login.html";
        }
        String entityJson = (String) r.get("entity");
        MemberDTO memberVO = JSON.parseObject(entityJson, MemberDTO.class);
        session.setAttribute(AuthConstant.AUTH_SESSION_REDIS,memberVO);
        // 注册成功就需要调整到商城的首页
        return "redirect:http://mall.piik.com/home";
    }

    /**
     * 百度账号登录
     * @param code
     * @param session
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("/oauth2/baidu/success")
    public String baiduOAuth(@RequestParam("code") String code
            , HttpSession session, HttpServletResponse response) throws Exception {
        System.out.println("code = " + code);
        Map<String,String> body = new HashMap<>();
        body.put("client_id","8OkQTGuVvXsXXG9FBKiPg0HD");
        body.put("client_secret","OAcVcAAoYtRpCDGpY3cU6GCpBCqIpqHV");
        body.put("grant_type","authorization_code");
        body.put("redirect_uri","http://auth.piik.com/oauth2/baidu/success");
        body.put("code",code);
        // 根据Code获取对应的Token信息
        HttpResponse post = HttpUtils.doPost("https://openapi.baidu.com"
                , "/oauth/2.0/token"
                , "post"
                , new HashMap<>()
                , null
                , body
        );
        int statusCode = post.getStatusLine().getStatusCode();
        if(statusCode != 200){
            // 说明获取Token失败,就调回到登录页面
            return "redirect:http://auth.piik.com/login.html";
        }
        // 说明获取Token信息成功
        String json = EntityUtils.toString(post.getEntity());
        SocialUserDTO socialUser = JSON.parseObject(json, SocialUserDTO.class);
        socialUser.setSocialType(2);
        R r = memberFeginService.socialLogin(socialUser);
        if(r.getCode() != 0){
            // 登录错误
            return "redirect:http://auth.piik.com/login.html";
        }
        String entityJson = (String) r.get("entity");
        MemberDTO memberVO = JSON.parseObject(entityJson, MemberDTO.class);
        session.setAttribute(AuthConstant.AUTH_SESSION_REDIS,memberVO);
        // 注册成功就需要调整到商城的首页
        return "redirect:http://mall.piik.com/home";
    }

}
